using System;
using System.Collections;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WorkflowActions;

namespace DP.Sharepoint.Workflow
{
    public partial class SendEmailExtended : Activity
    {
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context",
                                                                                         typeof (WorkflowContext),
                                                                                         typeof (SendEmailExtended));

        public static DependencyProperty BodyProperty = DependencyProperty.Register("Body", typeof (string),
                                                                                    typeof (SendEmailExtended));

        public static DependencyProperty IsMessageUrgentProperty = DependencyProperty.Register("IsMessageUrgent",
                                                                                               typeof (string),
                                                                                               typeof (SendEmailExtended
                                                                                                   ));

        public static DependencyProperty RecipientCCProperty = DependencyProperty.Register("RecipientCC",
                                                                                           typeof (ArrayList),
                                                                                           typeof (SendEmailExtended));

        public static DependencyProperty RecipientFromProperty = DependencyProperty.Register("RecipientFrom",
                                                                                             typeof (string),
                                                                                             typeof (SendEmailExtended));

        public static DependencyProperty RecipientTOProperty = DependencyProperty.Register("RecipientTO",
                                                                                           typeof (ArrayList),
                                                                                           typeof (SendEmailExtended));

        public static DependencyProperty SubjectProperty = DependencyProperty.Register("Subject", typeof (string),
                                                                                       typeof (SendEmailExtended));

        public SendEmailExtended()
        {
            InitializeComponent();
        }

        [Description("Recipient address")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ArrayList RecipientTO
        {
            get { return ((ArrayList) (base.GetValue(RecipientTOProperty))); }
            set { base.SetValue(RecipientTOProperty, value); }
        }

        [Description("Carbon copy recipient")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ArrayList RecipientCC
        {
            get { return ((ArrayList) (base.GetValue(RecipientCCProperty))); }
            set { base.SetValue(RecipientCCProperty, value); }
        }

        [Description("")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Subject
        {
            get { return ((string) (base.GetValue(SubjectProperty))); }
            set { base.SetValue(SubjectProperty, value); }
        }

        [Description("HTML body of the message")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Body
        {
            get { return ((string) (base.GetValue(BodyProperty))); }
            set { base.SetValue(BodyProperty, value); }
        }

        [Description("Determines whether message is priority is high")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string IsMessageUrgent
        {
            get { return ((string) (base.GetValue(IsMessageUrgentProperty))); }
            set { base.SetValue(IsMessageUrgentProperty, value); }
        }


        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext) (base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }

        [Description("Sender address. If this value is not specified, default sharepoint sender address will be used")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string RecipientFrom
        {
            get { return ((string) (base.GetValue(RecipientFromProperty))); }
            set { base.SetValue(RecipientFromProperty, value); }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                //need administrative credentials to get to Web Application Properties info
                SPSecurity.RunWithElevatedPrivileges(delegate
                                                         {
                                                             using (SPSite mySite = new SPSite(__Context.Site.ID))
                                                             {
                                                                 using (SPWeb myWeb = mySite.AllWebs[__Context.Web.ID])
                                                                 {
                                                                     string to = RecipientTO == null
                                                                                     ? string.Empty
                                                                                     : Common.ParseSendTo(__Context,
                                                                                                          RecipientTO);

                                                                     string cc = RecipientCC == null
                                                                                     ? string.Empty
                                                                                     : Common.ParseSendTo(__Context,
                                                                                                          RecipientCC);

                                                                     to = Common.ProcessStringField(executionContext, to);

                                                                     cc = Common.ProcessStringField(executionContext, cc);

                                                                     string from =
                                                                         Common.ProcessStringField(executionContext,
                                                                                                   RecipientFrom);

                                                                     string subject =
                                                                         Common.ProcessStringField(executionContext,
                                                                                                   Subject);

                                                                     string body =
                                                                         Common.ProcessStringField(executionContext,
                                                                                                   Body);

                                                                     Common.SendMailWithAttachment(mySite, from, to, cc,
                                                                                                   subject, body,
                                                                                                   new AttachmentInfo[0],
                                                                                                   bool.Parse(
                                                                                                       IsMessageUrgent));
                                                                 }
                                                             }
                                                         });
            }
            catch (Exception e)
            {
                Common.LogExceptionToWorkflowHistory(e, executionContext, WorkflowInstanceId);

                throw Common.WrapWithFriedlyException(e, "Error sending Email");
            }


            return base.Execute(executionContext);
        }
    }
}